package cn.zymall.common.utils.id;

import java.util.Random;
import java.util.UUID;

/**
 * @author xuxianda
 * @Description: ID工具类
 * @date 2020/8/21 11:54
 * @Copyright
 * @return
 **/
public class IDUtil {
    private static Random random = new Random();

    /**
     * 生成一串Long类型ID
     *
     * @return
     */
    public static Long getLongId() {

        return Math.abs(random.nextLong());
    }

    /**
     * 生成一串8位长度的UUID
     *
     * @return
     */
    public static String generate8BitUUID() {
        return getUUID().substring(0, 8);
    }

    /**
     * 生成一串12位长度的UUID
     *
     * @return
     */
    public static String generate12BitUUID() {
        return getUUID().substring(0, 12);
    }

    /**
     * 获取UUID并去除‘-’符合
     *
     * @return
     */
    private static String getUUID() {
        String uuid = UUID.randomUUID().toString();
        return uuid = uuid.replaceAll("-", "");
    }

    /**
     * 传入数字长度获取随机数
     * @param size 长度
     * @return
     */
    private static Integer getRandomNumber(Integer size) throws Exception {
        if (size == 0) {
            throw new RuntimeException("随机数长度不能为0");
        }
        return Math.abs(random.nextInt(size * 10));
    }
}
